<?php

namespace App\Http\Controllers\admin;

use DB;
use Illuminate\Http\Request;
use App\Http\Controllers\common\PublicController;

class AfficheController extends PublicController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function getIndex(Request $request)
    {
        $infomsg = Db::table('web')->first();
        $info = DB::table('notice')->orderBy('id','desc')->limit('10')->get();
        $data= array();
        foreach ($info as $k=>$v){
            $info['staff']=DB::table('staff')->where('id',$v['staff_id'])->select('name')->first();
            $info['datetime']=date('Y-m-d H:i:s', $v['datetime']);

            $data[] = [
              'id'=>$v['id'],
              'title'=>$v['title'],
              'content'=>$v['content'],
              'staff'=>$info['staff']['name'],
              'time'=>$info['datetime'],
            ];
        }
        return view('admin.affiche.index',['title'=>$infomsg['title'],'data'=>$data]);
    }

    public function getRead(Request $request){
        $id = $request->input('id');

       $info = DB::table('notice')->where('id',$id)->first();

        $info['staff']=DB::table('staff')->where('id',$info['staff_id'])->select('name')->first();
        $info['datetime']=date('Y-m-d', $info['datetime']);
        $data = [
            'id'=>$info['id'],
            'title'=>$info['title'],
            'content'=>$info['content'],
            'staff'=>$info['staff']['name'],
            'time'=>$info['datetime'],
        ];
//        dd($data);
        return view('admin.affiche.read',['data'=>$data]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function getCreate()
    {
        return view('admin.affiche.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function postStore(Request $request)
    {
        $info = $request -> except('_token');
        $info['datetime'] = time();
        //添加区域表
        $res = DB::table('notice') -> insert($info);
        if($res){
            $data['status'] = 1;
        }else{
            $data['status'] = -1;
        }
        echo json_encode($data);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function getEdit(Request $request)
    {
        $id = $request -> input('id');
        $data = DB::table('notice') -> where('id',$id) -> first();
        return view('admin.affiche.edit',['data'=>$data]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function postUpdate(Request $request)
    {
        $id = $request -> input('id');
        $info = $request -> except('id','_token');
        $res = DB::table('notice') -> where('id',$id) -> update($info);
        if($res){
            $data['status'] = 1;
        }else{
            $data['status'] = -1;
        }
        echo json_encode($data);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function postDelete(Request $request)
    {
        $id = $request -> input('id');
            //查询区域和客户的关系表并删除
            $res = DB::table('notice') -> delete($id);
            if($res){
                $data['status'] = 1;
            }else{
                $data['status'] = -1;
            }
        echo json_encode($data);
    }
}
